<?php
namespace Admin\Controller;
use Think\Controller;
//猜歌送红包
class LyricistController extends CommonController {

    public function user_list(){

        $User       =   M('lyricist_member'); // 实例化User对象

        $where = array();
        $where['uid'] = I('get.uid','','int');
        $where['prize_num'] = I('get.prize_num','','int');
        $nickname = I('get.nickname','','htmlspecialchars');

        if ($nickname){
            $where['nickname'] = array('like',"%$nickname%");
        }

        $where                              =   array_filter($where);

        $sum = array();

        if (!empty($where)){
            $field      =   array('uid','nickname','avatarurl','province','city','game_num','prize_num','totle_num','money_total','total_money','lock_money','login_num','create_time','login_time','appid');

            $count      =   $User->where($where)->count();// 查询满足要求的总记录数
            $Page       =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       =   $Page->show();// 分页显示输出

            $data['user_list'] = $User->field($field)->where($where)->order('uid desc')->limit($Page->firstRow.','.$Page->listRows)->select();

            foreach ($data['user_list'] as $key=>$val){

                $data['user_list'][$key]['frendnum'] = M('lyricist_relationship')->where(array('my_member_id'=>$val['uid']))->count();
                $data['user_list'][$key]['sharegroup']  = M('lyricist_wechat_group')->where(array('uid'=>$val['uid']))->count();

                if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                    $data['user_list'][$key]['title'] = '疯狂填词王奖金版';
                }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                    $data['user_list'][$key]['title'] = '成语猜猜看奖金版';
                }

                $sum['frendnum'] += $data['user_list'][$key]['frendnum'];
                $sum['sharegroup'] += $data['user_list'][$key]['sharegroup'];
                $sum['game_num'] +=$val['game_num'];
                $sum['prize_num'] +=$val['prize_num'];
                $sum['totle_num'] +=$val['totle_num'];


                $sum['money_total'] +=$val['money_total'];

                $sum['total_money'] +=$val['total_money'];

                $sum['lock_money'] +=$val['lock_money'];
            }
            $sum['money_total'] = sprintf("%.2f",$sum['money_total']);
            $sum['total_money'] = sprintf("%.2f", $sum['total_money']);

            $this->assign('page',$show);// 赋值分页输出
            $this->assign("data",$data);
            $this->assign("sum",$sum);
            $this->display('user_list');
        }else {
            $count      =   $User->count();// 查询满足要求的总记录数
            $Page       =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       =   $Page->show();// 分页显示输出

            $field      =   array('uid','nickname','avatarurl','province','city','game_num','prize_num','totle_num','money_total','total_money','lock_money','login_num','create_time','login_time','appid');
            $data['user_list'] = $User->field($field)->order('uid desc')->limit($Page->firstRow.','.$Page->listRows)->select();

            foreach ($data['user_list'] as $key=>$val){

                $data['user_list'][$key]['frendnum'] = M('lyricist_relationship')->where(array('my_member_id'=>$val['uid']))->count();
                $data['user_list'][$key]['sharegroup']  = M('lyricist_wechat_group')->where(array('uid'=>$val['uid']))->count();

                if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                    $data['user_list'][$key]['title'] = '疯狂填词王奖金版';
                }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                    $data['user_list'][$key]['title'] = '成语猜猜看奖金版';
                }

                $sum['frendnum'] += $data['user_list'][$key]['frendnum'];
                $sum['sharegroup'] += $data['user_list'][$key]['sharegroup'];
                $sum['game_num'] +=$val['game_num'];
                $sum['prize_num'] +=$val['prize_num'];
                $sum['totle_num'] +=$val['totle_num'];

                $sum['money_total'] +=$val['money_total'];

                $sum['total_money'] +=$val['total_money'];

                $sum['lock_money'] +=$val['lock_money'];
            }
            $sum['money_total'] = sprintf("%.2f",$sum['money_total']);
            $sum['total_money'] = sprintf("%.2f", $sum['total_money']);

            $this->assign("sum",$sum);
            $this->assign('page',$show);// 赋值分页输出
            $this->assign("data",$data);
            $this->display('user_list');
        }



    }

    //分享微信群
    public function wechat_group()
    {
        $Module         =   M('lyricist_wechat_group'); // 实例化对象
        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array();
        $order_by['id'] =   'desc';
        $data['wechat_group']       =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($data['wechat_group'] as $key=>$val){

            if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                $data['wechat_group'][$key]['title'] = '疯狂填词王奖金版';
            }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                $data['wechat_group'][$key]['title'] = '成语猜猜看奖金版';
            }
        }


        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);

        $this->display('wechat_group');
    }

    /**
     * 投诉信息
     */
    public function tousu(){

        $Module         =   M('lyricist_tousu'); // 实例化对象
        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出


        $field          =   array();
        $order_by['id'] =   'desc';
        $data      =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($data as $key=>$val){

            if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                $data[$key]['biaoqian'] = '疯狂填词王奖金版';
            }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                $data[$key]['biaoqian'] = '成语猜猜看奖金版';
            }
        }


        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);

        $this->display('tousu');


    }

    //礼物领取记录(红包)
    public function game_redbag()
    {

        $where = array();
        $where['member_id'] = I('get.uid','','int');
        $where = array_filter($where);

        $Module         =   M('lyricist_redbag'); // 实例化对象

        if (empty($where)){
            $count          =   $Module->count();// 查询满足要求的总记录数
        }else {
            $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        }


        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array('challenge_id','red_total','create_time','update_time','status','total_sn','member_id','appid');
        $order_by['challenge_id']   =   'desc';

        if (empty($where)){
            $data['lyricist_redbag']     =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $data['lyricist_redbag']     =   $Module->field($field)->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();
        }

        $sum = 0;
        foreach ($data['lyricist_redbag'] as $key=>$val){

            if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                $data['lyricist_redbag'][$key]['title'] = '疯狂填词王奖金版';
            }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                $data['lyricist_redbag'][$key]['title'] = '成语猜猜看奖金版';
            }
            $sum += $val['red_total'];
        }

        $sum = sprintf("%.2f", $sum);

        $this->assign('page',$show);// 赋值分页输出

        $this->assign('sum',$sum);// 列总钱数

        $this->assign("data",$data);

        $this->display('game_redbag');
    }

    //审核中
    public function game_cash()
    {

        if (IS_AJAX){

            $where['id'] = I('post.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $user = M('lyricist_cash')->field('uid,money')->where($where)->find();

            $uid = $user['uid'];
            $update['cash_status'] = 1;
            $update['update_time'] = time();
            $upchsh = M('lyricist_cash')->where($where)->save($update);

            M('lyricist_member')->where(array('uid'=>$uid))->save(array('lock_money'=>'0'));
            M('lyricist_member')->where(array('uid'=>$uid))->setInc('used_money',$user['money']);



            if (!empty($upchsh))
            {
                $msg['state']   =   200;
                $msg['error']   =   '成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '失败';
                $this->ajaxReturn ($msg,'JSON');
            }


        }else{

            $Module         =   M('lyricist_cash'); // 实例化对象
            $where['cash_status'] = 0;
            $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
            $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show           =   $Page->show();// 分页显示输出

            $order_by['id']   =   'asc';
            $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

            foreach ($data as $key=>$val){

                if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                    $data[$key]['title'] = '疯狂填词王奖金版';
                }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                    $data[$key]['title'] = '成语猜猜看奖金版';
                }
            }

            $this->assign('page',$show);// 赋值分页输出
            $this->assign('title','疯狂填词');
            $this->assign("data",$data);
            $this->display('game_cash');
        }
    }

    //审核过
    public function game_cash1()
    {

        $Module         =   M('lyricist_cash'); // 实例化对象
        $where['cash_status'] = 1;
        $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

        foreach ($data as $key=>$val){

            if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                $data[$key]['title'] = '疯狂填词王奖金版';
            }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                $data[$key]['title'] = '成语猜猜看奖金版';
            }
        }

        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash1');
    }

    //所有
    public function game_cash2()
    {

        $Module         =   M('lyricist_cash'); // 实例化对象

        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($data as $key=>$val){

            if ($val['appid'] =='wx402ce3c8d9a9de9b'){
                $data[$key]['title'] = '疯狂填词王奖金版';
            }elseif ($val['appid'] =='wx7d979a630d92c3dd'){
                $data[$key]['title'] = '成语猜猜看奖金版';
            }
        }

        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash2');
    }

    public function config(){

        $data = M('lyricist_config')->where(array('category'=>'lyricist'))->find();

        $this->assign('data',$data);
        $this->display('config');
    }


    public function config_edit()
    {
        $where['id'] =   I('post.id','','htmlspecialchars');
        $data['play_num']     =   I('post.play_num','','htmlspecialchars');
        $data['sharetwo']         =   I('post.sharetwo','','htmlspecialchars');
        $data['sharetitle']          =   I('post.sharetitle','','htmlspecialchars');
        $data['isfree']          =   I('post.isfree','','htmlspecialchars');

        //安全判断
        $result     =   M('lyricist_config')->where($where)->save($data); // 根据条件更新记录

        //记录相关操作日志

        if (!empty($result))
        {
            $msg['state']   =   200;
            $msg['error']   =   '更新成功';
            $this->ajaxReturn ($msg,'JSON');
        }
        else
        {
            $msg['state']   =   -1;
            $msg['error']   =   '更新失败';
            $this->ajaxReturn ($msg,'JSON');
        }
    }


    public function send(){

        echo '开发中';
    }

}